package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.MallPeripheryDetailDtoMapper;
import com.starlink.model.domain.MallPeripheryDetailDO;
import com.starlink.mapper.MallPeripheryDetailMapper;
import com.starlink.model.request.MallPeripheryDetailRequest;
import com.starlink.service.MallPeripheryDetailService;
import com.starlink.model.util.MallPeripheryDetailConvert;
import com.starlink.model.vo.MallPeripheryDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.starlink.utils.IDUtils.getId;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class MallPeripheryDetailServiceImpl extends ServiceImpl<MallPeripheryDetailMapper, MallPeripheryDetailDO> implements MallPeripheryDetailService {
    @Autowired
    private MallPeripheryDetailMapper mallPeripheryDetailMapper;

    @Override
    public MallPeripheryDetailVO getByKey(long id) {
        MallPeripheryDetailVO mallPeripheryDetailVO = MallPeripheryDetailDtoMapper.INSTANCE.do2VO(getById(id));
        return mallPeripheryDetailVO;
    }

    @Override
    public PageInfo<MallPeripheryDetailVO> pageQuery(Pageable pageable,MallPeripheryDetailRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<MallPeripheryDetailDO> list = mallPeripheryDetailMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<MallPeripheryDetailVO> mallPeripheryDetailVOList = MallPeripheryDetailConvert.buildMallPeripheryDetailVoList(list);
        return new PageInfo(mallPeripheryDetailVOList);
    }

    @Override
    public List<MallPeripheryDetailVO> listQuery(MallPeripheryDetailRequest request) {
        List<MallPeripheryDetailDO> list = mallPeripheryDetailMapper.selectDOList(MallPeripheryDetailDtoMapper.INSTANCE.request2DO(request));
        List<MallPeripheryDetailVO> mallPeripheryDetailVOS = MallPeripheryDetailConvert.buildMallPeripheryDetailVoList(list);
        return mallPeripheryDetailVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(MallPeripheryDetailRequest request) {
        request.setId(getId());
        save(MallPeripheryDetailDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(MallPeripheryDetailRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(MallPeripheryDetailDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        mallPeripheryDetailMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateByPerId(String peripheryInfo, Long id) {
        mallPeripheryDetailMapper.updateByPerId(peripheryInfo,id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletedByPerIds(String ids) {
        mallPeripheryDetailMapper.deletedByPerIds(Convert.toStrArray(ids));
    }
}
